/**

 * RetrieveMore.java
 *
 */

package com.samples.moreSample;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.blackberry.api.mail.*;
import net.rim.blackberry.api.mail.event.*;

class RetrieveMore  implements MessageListener
{
    private int messageID;

    public RetrieveMore()
    {
    }

    //Locate a message that has more available and request more.

    private void getMessage()
    {
        Store store = null;
        Folder folder = null;

        try
        {
            //Get the default mail session.
            store = Session.waitForDefaultSession().getStore();
        }
        catch (NoSuchServiceException ex)
        {
            //No message services found!
            System.out.println(ex.toString() + ": " + ex.getMessage());
        }

        try
        {
            //Open the Inbox for the default mail session.
            //Open either the BWC or BES Inbox.
            //BWC Inbox
            //folder = store.getFolder("Web Client/Inbox");
            //BES Inbox
            folder = store.getFolder("Inbox");
        }
        catch (FolderNotFoundException ex)
        {
            //The Inbox was not found!
            System.out.println(ex.toString() + ": " + ex.getMessage());
        }

            Message[] msgs = null;

        try
        {
            //Get all messages from the Inbox.
            msgs = folder.getMessages();
        }
        catch (MessagingException ex)
        {
            System.out.println(ex.toString() + ": " + ex.getMessage());
        }

        BodyPart bp;
        boolean moreMessage = false;

        //Loop until we find a message with more available.
        for (int count = 0; count < msgs.length; ++count)
        {
            bp = (BodyPart)msgs[count].getContent();
            if (( bp.hasMore() ) && (!bp.moreRequestSent()))
            {
                //A message with more available has been found!
                moreMessage = true;
                //Save the messageID
                messageID = msgs[count].getMessageId();

                System.out.println("The body size of the message before more is: "
                    + getBodyContent(bp).length());

                //getBodyText can also be used to retreive a String based body of a message.
                System.out.println("The body size of the message before more is: "
                    + msgs[count].getBodyText().length());

                System.out.println("Requesting more for MessageID " + messageID);

                //Request more, specify true in the second parameter to request the rest of
                //the message body (up to a maximum of 32 kb).
                //Note that BWC does not support the true parameter of a more request.
                try
                {
                    Transport.more(bp, true);
                }
                catch (MessagingException ex)
                {
                    System.out.println(ex.toString() + ": " + ex.getMessage());
                }

                //Add a message listener to this message so the application will
                //receive notification when the message has been changed.
                msgs[count].addMessageListener(this);
                break;
            }
        }

        if (!moreMessage)
            Dialog.alert("No messages with more available were found in the Inbox.");
    }

    //Returns a String obtained from the contents of all BodyParts
    //of a Multipart message.
    private String getBodyContent(BodyPart bp)
    {
        //Get the parent Multipart of the BodyPart of this message.
        Multipart mp = bp.getParent();
        StringBuffer sb = new StringBuffer();

        //Loop through all child BodyParts of the Multipart.
        for (int mpCount = 0; mpCount < mp.getCount(); ++mpCount)
        {
            //Assuming the message is text based TextBodyPart can be used.
            TextBodyPart tp = (TextBodyPart)mp.getBodyPart(mpCount);
            //Append the content of each BodayPart to a StringBuffer.
            sb.append((String)tp.getContent());
        }
        return sb.toString();
    }

    //Invoked when a Message with a MessageListener has been changed.
    public void changed(MessageEvent eve)
    {
        final Message msg = eve.getMessage();
        System.out.println("*Message event for messageID: " + msg.getMessageId());
        //Ensure the message is the same one that was recorded above.
        if (eve.getMessageChangeType() == MessageEvent.UPDATED
            && msg.getMessageId() == messageID)
        {
            System.out.println("More has been received!");

            BodyPart bp = (BodyPart)msg.getContent();
            System.out.println("The body size of this message after more is: "
                + getBodyContent(bp).length());

            //getBodyText can also be used to retreive a String based body of a message.
            System.out.println("The body size of this message after more is: "
                + msg.getBodyText().length());
        }
    }

